package cn.baker.app.consmer;

import cn.baker.app.common.Constants;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitListenerConsumer {

    @RabbitListener(containerFactory = "",
            bindings = @QueueBinding(
                    value = @Queue(value = Constants.FANOUT_QUEUE_NAME, durable = "true"),
                    exchange = @Exchange(type = ExchangeTypes.FANOUT, value = Constants.FANOUT_EXCHANGE, durable = "true")),
            concurrency = "${fanout.consumer.count:2}")
    public void receiveFanoutMessage(Message message) {
        String[] beanNamesForType = SpringUtil.getBeanNamesForType(ConnectionFactory.class);

        log.info("receive fanout message payload={}", JSON.toJSONString(new String(message.getBody())));
    }

}
